package com.sls.web.manager.game.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.DownstreamUserInfo;
import com.sls.web.manager.game.querybean.DownstreamUserQuery;
import com.sls.web.manager.game.service.DownstreamUserService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

/**
 * 下游用户信息操作
 * 
 * @author cx
 */
@Repository("downstreamUserService")
public class DownstreamUserServiceImpl extends SuperService implements
		DownstreamUserService {
	private Gson gson;

	public DownstreamUserServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean save(DownstreamUserInfo userInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DOWN_STREAM_USER_SAVE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		userInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, userInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean update(DownstreamUserInfo userInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DOWN_STREAM_USER_UPDATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		userInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, userInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public DownstreamUserInfo queryDownstreamUserByUserNo(String userNo,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DOWN_STREAM_USER_QUERY_BY_USERNO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		DownstreamUserInfo userInfo = new DownstreamUserInfo();
		userInfo.setUserNo(userNo);
		SocketDTO dto = super.send(head, userInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				userInfo = gson.fromJson(body, DownstreamUserInfo.class);
				return userInfo;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public DownstreamUserInfo queryDownstreamUserById(String id,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DOWN_STREAM_USER_QUERY_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		DownstreamUserInfo userInfo = new DownstreamUserInfo();
		userInfo.setId(id);
		SocketDTO dto = super.send(head, userInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				userInfo = gson.fromJson(body, DownstreamUserInfo.class);
				return userInfo;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<DownstreamUserInfo> queryDownstraeamUserInfo(
			DownstreamUserQuery userQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DOWN_STREAM_USER_PAGE_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, userQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<DownstreamUserInfo> page = gson.fromJson(body,
					new TypeToken<Pagination<DownstreamUserInfo>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<DownstreamUserInfo> queryAllDownUserInfo(UserInfoVO userInfoVO)
			throws Exception {
		DownstreamUserQuery query = new DownstreamUserQuery();
		query.setCurrentPage(1);
		query.setPageSize(20);

		List<DownstreamUserInfo> userList = new LinkedList<DownstreamUserInfo>();
		Pagination<DownstreamUserInfo> page = queryDownstraeamUserInfo(query,
				userInfoVO);
		if (page != null) {
			if (page.getList() != null) {
				userList.addAll(page.getList());
			}
			int count = page.getRowCount();
			if (query.getPageSize() == 0) {
				query.setPageSize(1);
			}
			for (int i = 1; i < Math.ceil(count * 1.0 / query.getPageSize()); i++) {
				query.setCurrentPage(i + 1);
				page = queryDownstraeamUserInfo(query, userInfoVO);
				if (page.getList() != null) {
					userList.addAll(page.getList());
				}
			}
			return userList;
		}
		return null;

	}

}
